package org.example;

import lombok.extern.slf4j.Slf4j;
import org.example.Impl.UserServiceImpl;
import org.example.config.RegistryConfig;
import org.example.config.RpcConfig;
import org.example.model.ServiceMetaInfo;
import org.example.registry.LocalRegistry;
import org.example.registry.Registry;
import org.example.registry.RegistryFactory;
import org.example.server.HttpServer;
import org.example.server.VertxHttpServer;
import org.example.server.tcp.VertxTcpServer;
import org.example.service.UserService;

/**
 * Hello world!
 *
 */
@Slf4j
public class EasyProviderApp
{
    public static void main( String[] args )
    {
//        RpcConfig rpcConfig = RpcApp.getRpcConfig();
//        LocalRegistry.register(UserService.class.getName(), UserServiceImpl.class);
//        HttpServer httpServer = new VertxHttpServer();
//        httpServer.doStart(rpcConfig.getServerPort());
//        System.out.println( "Hello World!" );

        RpcApp.init();

        String serviceName = UserService.class.getName();
        LocalRegistry.register(serviceName, UserServiceImpl.class);

        // 注册到服务中心
        RpcConfig rpcConfig = RpcApp.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());


        ServiceMetaInfo serviceMetaInfo = ServiceMetaInfo.builder()
                .serviceName(serviceName)
                .serviceHost(rpcConfig.getServerHost())
                .servicePort(rpcConfig.getServerPort())
                .build();
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            log.info("注册节点元信息错误");
            throw new RuntimeException(e);
        }

//        // 启动Http web服务
//        HttpServer httpServer = new VertxHttpServer();
//        httpServer.doStart(rpcConfig.getServerPort());
        // 启动Tcp服务
        VertxTcpServer vertxTcpServer = new VertxTcpServer();
        vertxTcpServer.doStart(rpcConfig.getServerPort());
    }
}
